<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 14:20
 */

namespace app\controller\index\pay;


use app\controller\index\BaseController;
use app\exception\IndexException;
use app\logic\index\pay\PayLogic;
use app\logic\index\pay\WeixinPayLogic;
use support\Request;
use support\Response;

class IndexController extends BaseController
{
    public function getReproductList(Request $request):Response
    {
        $param = [];
        $param['platform'] = $request->input('platform','ios');
        $param['name'] = $request->input('name');
        $data = PayLogic::getInstance()->getReproductList($param);
        return $this->endSuccess($data);
    }

    public function getVipproductList(Request $request):Response
    {
        $param = [];
        $param['platform'] = $request->input('platform','ios');
        $param['name'] = $request->input('name');
        $data = PayLogic::getInstance()->getVipproductList($param);
        return $this->endSuccess($data);

    }

    /**
     * 苹果支付验证
     */
    public function applePay(Request $request):Response
    {
        $param = [];
        $param['trade_no'] = $request->input('trade_no', '');
        $param['receipt_data'] = $request->input('receipt_data', '');
        $param['is_reply'] = $request->input('is_reply', '0');

        if (strlen($param['receipt_data']) < 40) {
            throw new IndexException(2008,'验证参数错误');
        }

        if (empty($param['trade_no'])) {
            //throw new IndexException(2008,'交易号必传');
        }

        //检查订单
        $res = PayLogic::getInstance()->applePay($param);

        return $this->endSuccess($res);
    }

    /**
     * 微信支付
     */
    public function weixinPay(Request $request):Response
    {
        $param = [];
        $param['product_id'] = $request->input('product_id');
        $param['type'] = $request->input('type',1);
        if (empty($param['product_id'])) {
            throw new IndexException(4020,'产品Id必传');
        }
        $result = WeixinPayLogic::getInstance()->weChatPay($param);

        return $this->endSuccess($result);
    }
}